package com.hryk.medical.login.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baoteng.base.component.BaseActivity;
import com.baoteng.base.view.TopTitleBar;
import com.hryk.medical.R;
import com.hryk.medical.common.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: loren
 * Date: 2019/9/24
 */
public class LoginActivity extends BaseActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private ViewPagerAdapter mAdapter;
    private LoginTabHolder mHolder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        initView();
    }

    @Override
    public void initTitle(TopTitleBar topTitleBar) {
        topTitleBar.setVisibility(View.GONE);
    }

    private void initView() {
        mTabLayout = findViewById(R.id.login_type_tab);
        mViewPager = findViewById(R.id.login_type_pager);

        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(new LoginPhoneFragment());
        mFragments.add(new LoginAccountFragment());
        mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        setupTabView(mTabLayout);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTab(tab, true);
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTab(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void setupTabView(TabLayout tabLayout) {
        String[] titles = new String[]{getString(R.string.login_tab_phone), getString(R.string.login_tab_account)};
        int[] images = new int[]{R.mipmap.login_tab_phone_icon, R.mipmap.login_tab_account_icon};
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.item_login_tab);
                View view = tab.getCustomView();
                if (view != null) {
                    mHolder = new LoginTabHolder(view);
                    mHolder.tabTv.setText(titles[i]);
                    mHolder.tabImage.setImageResource(images[i]);
                }
            }
            if (i == 0) {
                mHolder.tabLl.setSelected(true);
            }
        }
    }

    private void changeTab(TabLayout.Tab tab, boolean isSelected) {
        View customView = tab.getCustomView();
        if (customView == null) {
            return;
        }
        mHolder = new LoginTabHolder(customView);
        mHolder.tabTv.setSelected(isSelected);
    }

    static class LoginTabHolder {
        LinearLayout tabLl;
        TextView tabTv;
        ImageView tabImage;

        LoginTabHolder(View tabView) {
            tabLl = tabView.findViewById(R.id.login_tab_ll);
            tabTv = tabView.findViewById(R.id.login_tab_text);
            tabImage = tabView.findViewById(R.id.login_tab_image);
        }
    }
}
